import { observable, action, computed } from 'mobx';
import { message } from 'antd';
import * as API from '../../apis/performance/scoreWorkerReportApi'
import { idsAndNamesToArr } from '../../util/Util'
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-考核结果分析-按人员
 * jiansong 2019/7/3
 */
export default class ScoreWorkerReportStore {

    @observable loading = true; // 页面初始化的loading状态：数据加载成功前后前使用
    @observable hasScore = true; // 是否有分数段
    @observable hasRight = false; // 判断用户是有权限查看当前页面： 没有权限渲染无权限页面，有权限渲染数据
    @observable data = []; //表格数据
    @observable moreColumns = []; //表格多余的字段
    @observable total = 0; //数据数量
    @observable showSearchAd = false; // 高级搜索面板显示
    @observable yearDatas = []; //年份数组

    @computed get hrmData() { //分部回显数据
        return idsAndNamesToArr(this.queryParams.hrmids, this.queryParams.hrmnames);
    }
    @computed get subcompanyData() { //分部回显数据
        return idsAndNamesToArr(this.queryParams.subcompanyids, this.queryParams.subcompanynames);
    }
    @computed get departmentData() { //部门回显数据
        return idsAndNamesToArr(this.queryParams.departmentids, this.queryParams.departmentnames);
    }

    @observable includesubData = [
        { key: "3", showname: getLabel('508780', '含子分部') },
        { key: "1", showname: getLabel('18919', '仅本分部') },
        { key: "2", showname: getLabel('18920', '仅子分部') },
    ];
    @observable includedeptData = [
        { key: "3", showname: getLabel('508784', '含子部门') },
        { key: "1", showname: getLabel('18916', '仅本部门') },
        { key: "2", showname: getLabel('18917', '仅子部门') },
    ];

    @observable queryParams = {  //查询条件
        year: "",//年份
        hrmids: null, //人员
        hrmnames: null, //人员名称
        includesub: "3", //是否包含分部，默认含子分部
        subcompanyids: null, //分部
        subcompanynames: null, //分部名称
        includedept: "3", //是否包含部门，默认含子部门
        departmentids: null, //部门
        departmentnames: null, //部门名称

        //分页参数
        pageSize: 10, //每页显示数量
        current: 1, //当前页数
        sort: 'asc', //排序id字段
    };

    @action("初始化数据")
    doInit = () => {
        //获取当前年份
        let date = new Date();
        let fullYear = date.getFullYear();
        let ydatas = [];
        for (let i = parseInt(fullYear)+1; i > 2012; i--) {
            ydatas.push({ key: i, showname: i });
        }
        //默认选中当前年份
        this.yearDatas = ydatas;
        this.queryParams = {  //查询条件
            year: fullYear.toString(),//年份
            hrmids: null, //人员
            hrmnames: null, //人员名称
            includesub: "3", //是否包含分部，默认含子分部
            subcompanyids: null, //分部
            subcompanynames: null, //分部名称
            includedept: "3", //是否包含部门，默认含子部门
            departmentids: null, //部门
            departmentnames: null, //部门名称

            //分页参数
            pageSize: 10, //每页显示数量
            current: 1, //当前页数
            sort: '', //排序id字段
        };
        this.getScoreWorkerReportData();
    }

    @action("搜索条件改变")
    onSearchChange = (params) => {
        for (let key in params) {
            this.queryParams[key] = params[key];
        }
    }

    @action("切换高级搜索框")
    setShowSearchAd = bool => this.showSearchAd = bool;

    @action("重置搜索条件")
    resetParams = () => {
        this.queryParams.hrmids = null;
        this.queryParams.hrmnames = null;
        this.queryParams.includesub = "3";
        this.queryParams.subcompanyids = null;
        this.queryParams.subcompanynames = null;;
        this.queryParams.includedept = "3";
        this.queryParams.departmentids = null;
        this.queryParams.departmentnames = null;
    }

    @action("搜索")
    advanceSearch = () => {
        this.queryParams.current = 1;
        this.showSearchAd = false;
        this.getScoreWorkerReportData();
    };

    @action("绩效-考核结果分析-按人员-列表")
    getScoreWorkerReportData = () => {
        this.loading = true;
        API.scoreWorkerReportApi(this.queryParams).then(action(res => {
            if (res.api_status) {
                this.data = res.data;
                this.total = res.total;
                this.moreColumns = res.columns;
                this.hasRight = res.hasRight;
                this.hasScore = res.hasScore;
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }))
    };

}
